//
//  PackageTableViewCell.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/20.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "PackageTableViewCell.h"
#import "UIImageView+Webcache.h"
#import "ProductInfo.h"

@interface PackageTableViewCell()

@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *sumLabel;
@property (strong, nonatomic) UIImageView *arrowImageView;
@property (strong, nonatomic) UIImageView *pictureImageView1;
@property (strong, nonatomic) UILabel *add1Label;
@property (strong, nonatomic) UIImageView *pictureImageView2;
@property (strong, nonatomic) UILabel *add2Label;
@property (strong, nonatomic) UIImageView *pictureImageView3;
@property (strong, nonatomic) UILabel *moneyLabel;

@end

@implementation PackageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(0.0f), TRANS_VALUE(230.0f), TRANS_VALUE(30.0f))];
        self.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
        self.titleLabel.textColor = I_COLOR_33BLACK;
        self.titleLabel.text = @"优惠套餐1";
        [self.contentView addSubview:self.titleLabel];
        
        self.sumLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(240.0f), TRANS_VALUE(0.0f), TRANS_VALUE(60.0f), TRANS_VALUE(30.0f))];
        self.sumLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
        self.sumLabel.textAlignment = NSTextAlignmentRight;
        self.sumLabel.textColor = I_COLOR_33BLACK;
        self.sumLabel.text = @"共3件";
        [self.contentView addSubview:self.sumLabel];
        
        self.arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(310.0f), TRANS_VALUE(10.0f), TRANS_VALUE(6.0f), TRANS_VALUE(10.0f))];
        self.arrowImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:self.arrowImageView];
        self.arrowImageView.image = [UIImage imageNamed:@"ic_me_arrow_black"];
        
        self.pictureImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(30.0f), TRANS_VALUE(50.0f), TRANS_VALUE(50.0f))];
        self.pictureImageView1.contentMode = UIViewContentModeScaleAspectFit;
        self.pictureImageView1.clipsToBounds = YES;
        self.pictureImageView1.layer.borderColor = I_COLOR_GRAY.CGColor;
        self.pictureImageView1.layer.borderWidth = 0.0f;
        [self.contentView addSubview:self.pictureImageView1];
        self.pictureImageView1.image = [UIImage imageNamed:@"ic_mobilephone_default1"];
        
        self.add1Label = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(60.0f), TRANS_VALUE(30.0f), TRANS_VALUE(30.0f), TRANS_VALUE(50.0f))];
        self.add1Label.font = [UIFont systemFontOfSize:TRANS_VALUE(12.0f)];
        self.add1Label.textAlignment = NSTextAlignmentCenter;
        self.add1Label.textColor = I_COLOR_33BLACK;
        self.add1Label.text = @"+";
        [self.contentView addSubview:self.add1Label];
        
        self.pictureImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(90.0f), TRANS_VALUE(30.0f), TRANS_VALUE(50.0f), TRANS_VALUE(50.0f))];
        self.pictureImageView2.contentMode = UIViewContentModeScaleAspectFit;
        self.pictureImageView2.clipsToBounds = YES;
        self.pictureImageView2.layer.borderColor = I_COLOR_GRAY.CGColor;
        self.pictureImageView2.layer.borderWidth = 0.0f;
        [self.contentView addSubview:self.pictureImageView2];
        self.pictureImageView2.image = [UIImage imageNamed:@"ic_mobilephone_default1"];
        
        self.add2Label = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(140.0f), TRANS_VALUE(30.0f), TRANS_VALUE(30.0f), TRANS_VALUE(50.0f))];
        self.add2Label.font = [UIFont systemFontOfSize:TRANS_VALUE(12.0f)];
        self.add2Label.textAlignment = NSTextAlignmentCenter;
        self.add2Label.textColor = I_COLOR_33BLACK;
        self.add2Label.text = @"+";
        [self.contentView addSubview:self.add2Label];
        
        self.pictureImageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(170.0f), TRANS_VALUE(30.0f), TRANS_VALUE(50.0f), TRANS_VALUE(50.0f))];
        self.pictureImageView3.contentMode = UIViewContentModeScaleAspectFit;
        self.pictureImageView3.clipsToBounds = YES;
        self.pictureImageView3.layer.borderColor = I_COLOR_GRAY.CGColor;
        self.pictureImageView3.layer.borderWidth = 0.0f;
        [self.contentView addSubview:self.pictureImageView3];
        self.pictureImageView3.image = [UIImage imageNamed:@"ic_mobilephone_default1"];
        
        self.moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(220.0f), TRANS_VALUE(30.0f), TRANS_VALUE(80.0f), TRANS_VALUE(50.0f))];
        self.moneyLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(12.0f)];
        self.moneyLabel.textAlignment = NSTextAlignmentRight;
        self.moneyLabel.numberOfLines = 0;
        self.moneyLabel.textColor = I_COLOR_RED;
        self.moneyLabel.text = @"套餐价:\n ￥3000";
        [self.contentView addSubview:self.moneyLabel];
        
    }
    self.contentView.backgroundColor = I_COLOR_WHITE;
    
    return self;
}

- (void)setPackageInfo:(PackageInfo *)packageInfo {
    _packageInfo = packageInfo;
    if(_packageInfo) {
        self.titleLabel.text = _packageInfo.packageDesc;
        self.pictureImageView1.hidden = YES;
        self.add1Label.hidden = YES;
        self.pictureImageView2.hidden = YES;
        self.add2Label.hidden = YES;
        self.pictureImageView3.hidden = YES;
        NSMutableArray *imageUrlArray = [NSMutableArray arrayWithCapacity:0];
        
        if(_packageInfo.productArray != nil) {
            for(int i = 0, n = (int)_packageInfo.productArray.count; i < n; i++) {
                ProductInfo *productInfo = [_packageInfo.productArray objectAtIndex:i];
                if(productInfo.imageUrl != nil) {
                    [imageUrlArray addObject:productInfo.imageUrl];
                }
            }
        }
        if(imageUrlArray.count > 2) {
            self.pictureImageView1.hidden = NO;
            [self.pictureImageView1 sd_setImageWithURL:[NSURL URLWithString:(NSString *)[imageUrlArray objectAtIndex:0]] placeholderImage:[UIImage imageNamed:@"ic_mobilephone_default1"]];
            self.add1Label.hidden = NO;
            self.pictureImageView2.hidden = NO;
            [self.pictureImageView2 sd_setImageWithURL:[NSURL URLWithString:(NSString *)[imageUrlArray objectAtIndex:1]] placeholderImage:[UIImage imageNamed:@"ic_mobilephone_default1"]];
            self.add2Label.hidden = NO;
            self.pictureImageView3.hidden = NO;
            [self.pictureImageView3 sd_setImageWithURL:[NSURL URLWithString:(NSString *)[imageUrlArray objectAtIndex:2]] placeholderImage:[UIImage imageNamed:@"ic_mobilephone_default1"]];
        } else if(imageUrlArray.count == 2) {
            self.pictureImageView1.hidden = NO;
            [self.pictureImageView1 sd_setImageWithURL:[NSURL URLWithString:(NSString *)[imageUrlArray objectAtIndex:0]] placeholderImage:[UIImage imageNamed:@"ic_mobilephone_default1"]];
            self.add1Label.hidden = NO;
            self.pictureImageView2.hidden = NO;
            [self.pictureImageView2 sd_setImageWithURL:[NSURL URLWithString:(NSString *)[imageUrlArray objectAtIndex:1]] placeholderImage:[UIImage imageNamed:@"ic_mobilephone_default1"]];
        } else if(imageUrlArray.count == 1) {
            self.pictureImageView1.hidden = NO;
            [self.pictureImageView1 sd_setImageWithURL:[NSURL URLWithString:(NSString *)[imageUrlArray objectAtIndex:0]] placeholderImage:[UIImage imageNamed:@"ic_mobilephone_default1"]];
        } else {
            self.add2Label.hidden = NO;
            self.add2Label.text = @"...";
        }
        self.sumLabel.text = [NSString stringWithFormat:@"共%ld件", [_packageInfo.productArray count]];
        self.moneyLabel.text = [NSString stringWithFormat:@"套餐价:\n￥%@", _packageInfo.combinePrice];
    }
}

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
